package com.heima.article.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class UpDownListener {
    @Autowired
    private IApArticleService articleService;
    @KafkaListener(topics = "${topic.upDownTopic}")
    public void handlerMsg(ConsumerRecord<String,String> consumerRecord){
        System.out.println("监听到消息,即将执行文章上下架操作!!!");
        LambdaUpdateWrapper<ApArticle> update = new LambdaUpdateWrapper<>();
        update.eq(ApArticle::getId, Long.valueOf(consumerRecord.key()));
        if ("false".equals(consumerRecord.value())){
            //下架
            update.set(ApArticle::getIsDown,1);
        }else if ("true".equals(consumerRecord.value())){
            //上架
            update.set(ApArticle::getIsDown,0);
        }
        articleService.update(update);

    }
}
